#include "client6.h"
#include "ui_client6.h"

client6::client6(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::client6)
{
    ui->setupUi(this);
    setWindowTitle(tr("小翼视频播放器"));
    //视频播放器操作
    player1=new QMediaPlayer(this);
    playlist1=new QMediaPlaylist(this);
    playlist1->setPlaybackMode(QMediaPlaylist::Loop);
    player1->setPlaylist(playlist1);

    vw=new QVideoWidget(this);
    ui->horizontalLayout->addWidget(vw);
    player1->setVideoOutput(vw);

    //视频播放器槽函数
    connect(player1,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));
    connect(player1,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64)));
}

client6::~client6()
{
    delete ui;
}
//添加视频
void client6::on_pushButton_addvideo_clicked_clicked()
{
    QStringList video_files = QFileDialog::getOpenFileNames(this,"选择视频文件","G:/【01回忆录】/小猫");
    //去重
    for(int i = 0;i<ui->listWidget1->count();i++)
    {
        for(int j = 0;j<video_files.count();j++)
        {
            if(ui->listWidget1->item(i)->text()==video_files.at(j))
            {
                video_files.removeAt(j);
                break;
            }
        }
    }
    if(video_files.isEmpty())
        return ;

    for(int i = 0;i<video_files.count();i++)
    {
        QString file =video_files.at(i);
        //文件加到播放列表
        playlist1->addMedia(QUrl::fromLocalFile(file));
    }
    ui->listWidget1->addItems(video_files);
    ui->listWidget1->setCurrentRow(0);

}
//删除视频
void client6::on_pushButton_deletevideo_clicked_clicked()
{
    int row = ui->listWidget1->currentRow();

    QListWidgetItem* item = ui->listWidget1->takeItem(row);

    if(item)
    {
        delete item;
    }

    playlist1->removeMedia(row);
}

void client6::on_pushButton_clearvideo_clicked_clicked()
{
    ui->listWidget1->clear();
    playlist1->clear();

}
//播放
void client6::on_pushButton_play_clicked_clicked()
{
    int row=ui->listWidget1->currentRow();
    playlist1->setCurrentIndex(row);
    player1->play();
    player1->setVolume(ui->verticalSlider->value());

    ui->pushButton_stop_cont->setText(tr("暂停"));
    ui->pushButton_stop_cont->setEnabled(true);

}

void client6::on_pushButton_cancel_clicked_clicked()
{
    player1->stop();
    ui->pushButton_stop_cont->setText(tr("暂停"));
    ui->pushButton_stop_cont->setEnabled(false);

}

void client6::on_pushButton_stop_cont_clicked_clicked()
{
    QString cmd="killall";
    QStringList args;

    if(ui->pushButton_stop_cont->text()=="暂停")
    {
        args<<"-STOP"<<"mplayer";
        ui->pushButton_stop_cont->setText("继续");
    }
    else
    {
        args<<"-CONT"<<"mplayer";
        ui->pushButton_stop_cont->setText("暂停");
    }
    p->execute(cmd,args);

}
//上一个
void client6::on_pushButton_prev_clicked_clicked()
{
    int row=ui->listWidget1->currentRow();
    if(row==0)
        row=ui->listWidget1->count();
    else
        row--;
    ui->listWidget1->setCurrentRow(row);
    playlist1->setCurrentIndex(row);
    player1->play();
    player1->setVolume(ui->verticalSlider->value());
    ui->pushButton_stop_cont->setText(tr("暂停"));
    ui->pushButton_stop_cont->setEnabled(true);

}
//下一个
void client6::on_pushButton_stop_2_clicked_clicked()
{
    int row=ui->listWidget1->currentRow();
    if(row==ui->listWidget1->count()-1)
        row=0;
    else
        row++;
    ui->listWidget1->setCurrentRow(row);
    playlist1->setCurrentIndex(row);
    player1->play();
    player1->setVolume(ui->verticalSlider->value());
    ui->pushButton_stop_cont->setText(tr("暂停"));
    ui->pushButton_stop_cont->setEnabled(false);

}

//双击播放
void client6::on_listw_doubleClicked(const QModelIndex &index)
{
    int row = ui->listWidget1->currentRow();
    playlist1->setCurrentIndex(row);
    player2->play();
    player2->setVolume(ui->verticalSlider->value());
    ui->pushButton_stop_cont->setText(tr("暂停"));
    ui->pushButton_stop_cont->setEnabled(true);
}

void client6::on_commandLinkButton_clicked()
{
    client3 *p = new client3;
    hide();
    p->show();

}
